//
//  ChaseViewController.m
//  Double
//
//  Created by 孟令博 on 15/5/25.
//  Copyright (c) 2015年 袁庆秋. All rights reserved.
//

#import "ChaseViewController.h"
#import "ChaseModel.h"
#import "ChaseViewCell.h"
#import "ChaseReusableView.h"
#import <AFNetworking/AFHTTPRequestOperationManager.h>
#import <UIImageView+WebCache.h>
#import "CartoonRecommendDetailViewController.h"

@interface ChaseViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, retain) NSMutableDictionary *dic1;
@property (nonatomic, retain) NSArray *array;
@property (nonatomic, retain) NSMutableArray *smallArray1;
@end

@implementation ChaseViewController

- (void)dealloc
{
   
    [_collection release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.dic1 = [NSMutableDictionary dictionary];
        self.array = [NSArray array];
        self.smallArray1 = [NSMutableArray array];

            }
    return self;
}
#pragma mark -
#pragma mark 网络请求
- (void)startConnection
{
    AFHTTPRequestOperationManager *m = [AFHTTPRequestOperationManager manager];
    m.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *str = [NSString stringWithFormat:@"http://manhua.haomee.cn/?m=Api&ios=23&pad=0&a=updateTime&need_sort=1"];
    m.responseSerializer = [AFHTTPResponseSerializer serializer];
    [m GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        self.array = [NSJSONSerialization JSONObjectWithData:data options:   NSJSONReadingMutableContainers error:nil];
                NSLog(@"%@", _array);
        
        for (NSDictionary *dic in self.array) {
            self.dic1 = [NSMutableDictionary dictionary];
            [_dic1 addEntriesFromDictionary:dic];
            NSString *str = [self.dic1 objectForKey:@"name"];
            [self.smallArray1 addObject:str];
            
        }
        
        [self.collection reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}

#pragma mark -
#pragma mark 定义collectionView
- (void)myCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width / 3 - 20, 130);
    layout.sectionInset = UIEdgeInsetsMake(10, 20, 10, 20);
    
    layout.headerReferenceSize = CGSizeMake(10, 30);
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height- 64) collectionViewLayout:layout];
    
    _collection.dataSource = self;
    _collection.delegate = self;
    
    [self.collection setBackgroundColor:[UIColor whiteColor]];
    
    [self.collection registerClass:[ChaseViewCell class] forCellWithReuseIdentifier:@"cell"];
    //注册section标题和结尾的重用池
    
    [self.collection registerClass:[ChaseReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [self.view addSubview:_collection];
    [layout release];
    [_collection release];
}
#pragma mark -
#pragma mark collection协议
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSMutableDictionary *dic = [self.array objectAtIndex:section];
    NSMutableArray *array = [dic objectForKey:@"list"];
    return [array count];
    
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    ChaseViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    NSMutableDictionary *dic1 = [self.array objectAtIndex:indexPath.section];
    NSMutableArray *array = [dic1 objectForKey:@"list"];
    NSMutableArray *bigArray = [NSMutableArray array];
    for (NSDictionary *dic in array) {
        ChaseModel *model = [[ChaseModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [bigArray addObject:model];
        
    }
    ChaseModel *model = [bigArray objectAtIndex:indexPath.item];
    
    cell.priceLable.text = model.name;
    
    [cell.myImage sd_setImageWithURL:[NSURL URLWithString:model.cover] placeholderImage:[UIImage imageNamed:@"placeholder.jpg"] options:SDWebImageRetryFailed];
    //判断是否连载
    if (model.is_over == 1) {
        
        cell.lable1.text = [NSString stringWithFormat:@"全%@集", model.cur_num];
        
    } else {
        cell.lable1.text = [NSString stringWithFormat:@"更新至第%@集", model.cur_num];
    }

    return cell;
}

#pragma mark -
#pragma mark section标题

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    
    return self.array.count;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
    CollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
    //        [header setBackgroundColor:[UIColor lightGrayColor]];
    
    header.alabel.text = [self.smallArray1 objectAtIndex:indexPath.section];
    return header;
}
#pragma mark -
#pragma mark cell点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    
    
    
    NSMutableDictionary *dic1 = [self.array objectAtIndex:indexPath.section];
    NSMutableArray *array = [dic1 objectForKey:@"list"];
    NSMutableArray *bigArray = [NSMutableArray array];
    for (NSDictionary *dic in array) {
        ChaseModel *model = [[ChaseModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [bigArray addObject:model];
        
    }
    ChaseModel *model = [bigArray objectAtIndex:indexPath.item];
    CartoonRecommendDetailViewController *cartoon = [[CartoonRecommendDetailViewController alloc] init];
    cartoon.iD = [NSString stringWithFormat:@"%ld", (long)model.video_id];
    cartoon.is_over = [NSString stringWithFormat:@"%ld", (long)model.is_over];
    cartoon.lastup = model.cur_num;
    cartoon.titleName = model.name;
    cartoon.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:cartoon animated:YES];
    [cartoon release];
    
}








- (void)viewDidLoad {
    [super viewDidLoad];
    [self myCollectionView];
    [self startConnection];
    
    //设置返回按钮
    UIButton *left = [UIButton buttonWithType:UIButtonTypeSystem];
    left.frame = CGRectMake(0, 0, 32, 32);
    [left setImage:[UIImage imageNamed:@"fanhui.png"] forState:UIControlStateNormal];
    [left addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:left];
    self.navigationItem.leftBarButtonItem = leftItem;
}

- (void)backAction:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
